//
//  JWTUpdateNameViewController.m
//  JWT
//
//  Created by ndm on 17/1/22.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTUpdateNameViewController.h"

@interface JWTUpdateNameViewController()
{
    //修改姓名的hintLabel
    UILabel *_hintUpNameLabel;
    //修改姓名的textfiled
    UITextField *_upNameTextField;
    //确认修改
    UIButton *_rightBtn;
    
}
@end


@implementation JWTUpdateNameViewController

-(instancetype)init{
    if ([super init]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithRightBtn];
    
    [self initWithAppearance];
}

-(void) initWithRightBtn{
    //保存按钮
    if (_rightBtn == nil) {
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 40, 30)];
        [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [rightBtn setTitleColor:RGB_WhiteBackGround forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(updateNameAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        self.navigationItem.rightBarButtonItem = rightBarItem;
        self.navigationController.navigationBar.tintColor = RGB(46, 46, 46);
        _rightBtn = rightBtn;
    }
}

-(void) updateNameAction:(UIButton *)sender{
    if ([_upNameTextField.text isEqualToString:APPDELEGATE.userInfo.name] || [_upNameTextField.text isEqualToString:@""]) {
        [HUD showToast:@"昵称未更改,请重置"];
        return;
    }else if([_upNameTextField.text length] >20) {
        [HUD showToast:@"长度不能超20,请重置"];
        return;
    } else{
        APPDELEGATE.userInfo.name = _upNameTextField.text;
        
        //修改姓名接口调用
        [self requestDataWithUpdateName];
    }
}

/**修改姓名的接口调用*/
-(void)requestDataWithUpdateName{
    [HUD showUIBlockingIndicator];
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] updateUserNameWithActionUpdateName];
    NSDictionary *dict = @{@"userid":APPDELEGATE.userInfo.userid,@"name":APPDELEGATE.userInfo.name,@"tokenid":APPDELEGATE.userInfo.tokenid};
    [AFNHttpsRequest requestWithUrl:urlString WithParam:dict WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"] integerValue] == 200) {
                [HUD showToast:[model objectForKey:@"message"]];
                [JWTUserHeaderView sharedInstance].userNameLabel.text = APPDELEGATE.userInfo.name;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh_updateUserInfoAction" object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [HUD showToast:[model objectForKey:@"message"]];
            }
        }
    }];
}



-(void) initWithAppearance{
    if (_upNameTextField == nil) {
        UITextField *upNameTextField = [[UITextField alloc] init];
        upNameTextField.frame = CGRectMake(self.view.left+2*Cell_Margin,  self.view.top+3*Cell_Margin, UISCREEN_WIDTH-4*Cell_Margin, 44);
        upNameTextField.layer.borderWidth = 0.5;
        upNameTextField.layer.borderColor = RGB_LightGray.CGColor;
        upNameTextField.layer.cornerRadius = upNameTextField.height*0.08;
        upNameTextField.backgroundColor = RGB_WhiteBackGround;
        upNameTextField.clearButtonMode = UITextFieldViewModeAlways;
        upNameTextField.font = [UIFont systemFontOfSize:14.0f];
        [self.view addSubview:upNameTextField];
        _upNameTextField = upNameTextField;
        _upNameTextField.text = APPDELEGATE.userInfo.name;
    }
}


@end
